home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / byte1286.arc / PERRY.ARC / CELLULAR.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1980-01-01  |  10.1 KB  |  485 lines

  1. program cellular;
  2. {one dimensional cellular automata }
  3. { PROCEDURES
  4. 1  procedure DisplayStatusLine;
  5. 2  procedure DisplayGenerations;
  6. 3  procedure ReadRuleFromFile; (not used in this program)
  7. 4  procedure GetRandomRule;
  8. 5  procedure ChangeRule;     (from keyboard)
  9. 6  procedure InitializeAinitToBackground;
  10. 7  procedure InitializeAinitRandom;
  11. 8  procedure MoveAinitToAfield;
  12. 9  procedure InitializeAinitFromKeyboard;
  13. 10 procedure SetBackground;
  14. 11 procedure StartFinish;
  15. 12 procedure Field80X47;
  16. 13 procedure Field160X95;
  17. 14 procedure Field320X190;
  18. 15 procedure ReadRuleAndAinitFromFile; 
  19.   (not used in this program)
  20. }
  21.  
  22. var
  23.   FilVar:    text;
  24.   Line:      string[20];
  25.   C:         string[1];
  26.  
  27.   Ainit:     array[0..4000] of byte; 
  28.    {4001 cells wide. Allows for }
  29.   Afield:    array[0..4000] of byte;     
  30.    { expansion of COMPUTE FIELD }
  31.   Bfield:    array[0..4000] of byte;
  32.  
  33.   Rule:        array[0..12] of byte;
  34.   I,J,M,N,H,P,V,X,Nix:  integer;
  35.   Ch:               char;
  36.  
  37.   Delta:    integer;  { pixel spacing  1, or 2 }
  38.   Dwidth:   Integer;  { width of display field }
  39.   Cwidth:   Integer;  { width of compute field }
  40.  
  41.   Cstart:   integer;  { COMPUTE FIELD. start with a }
  42.   Cfinish:  integer;  { width of 160 }
  43.  
  44.   Dstart:   integer;  { display field }
  45.   Dfinish:  integer;
  46.  
  47.   Vstart:   integer;  { vertical display }
  48.   Vfinish:  integer;
  49.  
  50.   Hstart:   integer;  { horizontal display }
  51.   Hfinish:  integer;
  52.  
  53. const { typed constants }
  54.       { these are essentially initialized variables }
  55.  
  56.   Widen:    Integer = 0;
  57.   Bgnd:     Integer = 0;
  58.  
  59.   k:        integer = 4;   { number of states }
  60.   RuleEnd:  integer = 9;   {  RuleEnd = 3 * (k - 1) }
  61.   r:        integer = 1;   { Range; number of neighbors }
  62.  
  63.  
  64.  
  65. const
  66.   Center = 2000;    {center of fields}
  67. { **********  start of procedures ******************* }
  68.  
  69. {----------------- 1 ----------------}
  70.  
  71. procedure DisplayMessage;
  72. begin
  73.   GoToXY(1,25);
  74.   Write('CELLULAR: by Kenneth E. Perry. 
  75.          Press Ins');
  76. end;
  77.  
  78. procedure DisplayStatusLine;
  79. begin
  80.   GoToXY(1,25);
  81.   Write('                                            ');
  82.   GoToXY(1,25);
  83.   Write(Rule[0]);
  84.   for I := 1 to 3 do
  85.   begin
  86.     write(' ');
  87.     for J := 1 to 3 do
  88.     begin
  89.       Write(Rule[3*(I-1)+J]);
  90.     end;
  91.   end;
  92.   Write('    ');  {4 spaces}
  93.   Write(Bgnd);
  94.   Write('    ');
  95.   Write(Cwidth);
  96. end;            {DisplayStatusLine}
  97.  
  98. {---------------- 2 -----------------}
  99.  
  100. procedure DisplayGenerations;
  101. { compute and display 190 generations 
  102. ( or rows of cells ) } 
  103. begin
  104.     for V := Vstart to Vfinish do    
  105.     { number of generations to display }
  106.     begin
  107.              { show display field }
  108.       if Delta = 1 then
  109.       begin
  110.         for H := Hstart to Hfinish do
  111.         begin
  112.           I := H + Dstart; { display one generation }
  113.           plot(H,V,Afield[I]);
  114.         end;
  115.       end;
  116.  
  117.       if Delta = 2 then
  118.       begin
  119.         for H := Hstart to Hfinish do
  120.         begin
  121.           I := H + Dstart;
  122.           plot(H+H,V+V,Afield[I]);
  123.         end;
  124.       end;
  125.  
  126.             { check for overflow of COMPUTE FIELD }
  127.  
  128.   if Widen = 1 then
  129.   begin
  130.   I := Cstart;
  131.   J := Cfinish;
  132.   if (Afield[I] <> Afield[I + 1]) or (Afield[J - 1] 
  133.   <> Afield[J]) then
  134.   begin
  135.     Cstart := Cstart - 1;   { this is to avoid end effects }
  136.      Cfinish := Cfinish + 1;
  137.      Cwidth := Cfinish - Cstart;
  138.    end;
  139.   end;
  140.  
  141.              {compute new row of cells and place in Bfield }
  142.  
  143.       for I := Cstart to Cfinish do
  144.       begin
  145.         N := Afield[I-1] + Afield[I] + Afield[I+1];
  146.         Bfield[I] := Rule[N];
  147.       end;
  148.  
  149.                {return Bfield to Afield}
  150.       for I := Cstart to Cfinish do
  151.       begin
  152.         Afield[I] := Bfield[I];
  153.       end;
  154.  
  155.     end; {for}
  156. end;  { DisplayGenerations }
  157.  
  158. {-------------------- 3 -------------------}
  159.  
  160.   procedure ReadRuleFromFile;
  161.   begin  {read rule from file 'DEMO-C.DOC' into 'Line'}
  162.     Readln(FilVar,Line);
  163.     GotoXY(1,25);
  164.     Writeln(Line); { display rule on bottom 
  165.                      line of screen }
  166.  J := 0;
  167.  for I := 1 to 13 do
  168.  begin
  169.    C := Copy(Line,I,1); { copy rule, one digit at a time }
  170.    if (C <> ' ') then   { skipping spaces }
  171.    begin
  172.      Val(C,M,Nix);
  173.         Rule[J] := M; { copy rule from 'Line' into 'Rule' }
  174.         J := J + 1;
  175.       end;
  176.     end;
  177.   end;    { ReadRuleFromFile }
  178.  
  179. {-------------------- 4 --------------------}
  180.  
  181.   procedure GetRandomRule;
  182.   begin
  183.     Rule[0] := 0;
  184.     Rule[1] := Random(k);
  185.     Rule[2] := Random(k);
  186.     Rule[3] := Random(k);
  187.     Rule[4] := Random(k);
  188.     Rule[5] := Random(k);
  189.     Rule[6] := Random(k);
  190.     Rule[7] := Random(k);
  191.     Rule[8] := Random(k);
  192.     Rule[9] := Random(k);
  193.   end;  { GetRandomRule }
  194.  
  195. {-------------------- 5 ---------------------}
  196.  
  197.   procedure ChangeRule;
  198.   begin
  199.     Rule[0] := 0;
  200.     GoToXY(3,25);
  201.     for i := 1 to 11 do
  202.     begin
  203.       Write(' ');
  204.     end;
  205.     GotoXY(3,25);
  206.     for I := 1 to RuleEnd do
  207.     begin
  208.      Read(Kbd,C);
  209.      Val(C,M,X);
  210.      Rule[I] := M;
  211.      Write(Rule[I]);
  212.     end;
  213.     DisplayStatusLine;
  214.   end;  { ChangeRule }
  215.  
  216. {-------------------- 6 ---------------------}
  217.  
  218. procedure InitializeAinitToBackground;
  219. begin
  220.   for I := 0 to 4000 do
  221.   begin
  222.     Ainit[I] := Bgnd;
  223.   end;
  224. end;
  225.  
  226. {-------------------- 7 ---------------------}
  227.  
  228. procedure InitializeAinitRandom;
  229. begin
  230.   { random initialize of COMPUTE FIELD in Ainit}
  231.     for I := Cstart to Cfinish do
  232.     begin
  233.       Ainit[I] := Random(k);
  234.     end;
  235. end;      { InitializeAinitRandom }
  236.  
  237. {-------------------- 8 ---------------------}
  238.  
  239. procedure MoveAinitToAfield;
  240. begin
  241.     for I := 0 to 4000 do
  242.     begin
  243.       Afield[I] := Ainit[I];
  244.     end;
  245. end;
  246.  
  247. {-------------------- 9 ----------------------}
  248.  
  249. procedure InitializeAinitFromKeyboard;
  250. begin
  251.   InitializeAinitToBackground;
  252.   GraphColorMode;
  253.   Delay(400);
  254.   DisplayStatusLine;
  255.   Plot(160,2,1); {display pixel cursor on "line 2" }
  256.   For I := 0 to (319 div Delta) do
  257.   begin
  258.     Plot(I*Delta,0,bgnd); { show background on "line 0" }
  259.   end;
  260.   M := Center;
  261.   N := 160 div Delta;
  262.   C := ' ';
  263.  
  264.   repeat
  265.     if keypressed then
  266.     begin
  267.       Read(Kbd,C);
  268.       if (C <> #27) and (C <> #42) then
  269.       begin
  270.  Plot(N * Delta,2,0); { erase pixel cursor }
  271.  val(C,P,Nix);        { C is String[1], P is integer }
  272.  Ainit[M] := P;
  273.  Plot(N * Delta,0,P);
  274.  M := M + 1;
  275.  N := N + 1;
  276.  Plot(N * Delta,2,1); { write new pixel cursor }
  277. end;
  278.  
  279.  
  280.   if (C = #27) and keypressed then
  281.   begin
  282.   Plot(N * Delta,2,0);
  283.   Read(Kbd,C);
  284.   if (C = #75) then     { left arrow }
  285.   begin
  286.           M := M - 1;
  287.           N := N - 1;
  288.         end;
  289.         if (C = #77) then     { right arrow }
  290.         begin
  291.           M := M + 1;
  292.           N := N + 1;
  293.         end;
  294.         Plot(N * Delta,2,1);
  295.       end;
  296.     end;
  297.   until (C = #42);     { * on keypad }
  298.  
  299.   Widen := 1;
  300.   MoveAinitToAfield;
  301.   DisplayGenerations;
  302.  
  303. end;      { InitializeAinitFromKeyboard }
  304.  
  305. {------------------- 10 -----------------}
  306.  
  307. procedure SetBackground;
  308. begin
  309.   read(Kbd,C);
  310.   Val(C,M,X);
  311.   Bgnd := M;
  312.   DisplayStatusLine;
  313. end;
  314.  
  315. {------------------- 11 -----------------}
  316.  
  317. procedure StartFinish;
  318. begin
  319.   Cstart := Center - (Cwidth div 2);
  320.   Cfinish := Center + (Cwidth div 2) - 1;
  321.   Dstart := Center - (Dwidth div 2);
  322.   Dfinish := Center + (Dwidth div 2) - 1;
  323. end;
  324.  
  325. {------------------ 12 ------------------}
  326.  
  327. procedure Field80X47;
  328. begin
  329.   GraphColorMode;
  330.   Dwidth := 80;
  331.   Cwidth := 80;
  332.  
  333.   StartFinish;
  334.  
  335.   Vstart := 0;
  336.   Vfinish := 48;
  337.   Hstart := 0;
  338.   Hfinish := 79;
  339.   Delta := 2;
  340.   Delay(400);
  341.   DisplayStatusLine;
  342. end;
  343.  
  344. {-------------------13 ------------------}
  345.  
  346. procedure Field160X95;
  347. begin
  348.   GraphColorMode;
  349.   Dwidth := 160;
  350.   Cwidth := 160;
  351.  
  352.   StartFinish;
  353.  
  354.   Vstart := 0;
  355.   Vfinish := 94;
  356.   Hstart := 0;
  357.   Hfinish := 159;
  358.   Delta := 2;
  359.   Delay(400);
  360.   DisplayStatusLine;
  361. end;
  362.  
  363. {-------------------14 -----------------}
  364.  
  365. procedure Field320X190;
  366. begin
  367.   GraphColorMode;
  368.   Dwidth := 320;
  369.   Cwidth := 320;
  370.  
  371.   StartFinish;
  372.  
  373.   Vstart := 0;
  374.   Vfinish := 189;
  375.   Hstart := 0;
  376.   Hfinish := 319;
  377.   Delta := 1;
  378.   Delay(400);
  379.   DisplayStatusLine;
  380. end;
  381.  
  382.  
  383.  
  384.  
  385. { *************   end  of procedures ***************** }
  386.  
  387.  
  388.  
  389. { ************** MAIN PROGRAM ************************ }
  390.  
  391.  
  392. begin
  393.  
  394. Ch := ' ';
  395. GraphColorMode;
  396. Palette(0);
  397. Randomize;
  398. Field160X95;
  399. DisplayMessage;
  400.  
  401. repeat
  402.   if KeyPressed then
  403.   begin               {keypad symbols}
  404.     Read(Kbd,Ch);
  405.     if (Ch = #45) then             { - }
  406.     begin
  407.       InitializeAinitFromKeyboard
  408.     end;
  409.  
  410.  
  411.     if (Ch = #43) then            { + }
  412.     begin                 { Continue Structure }
  413.       DisplayStatusLine;
  414.       DisplayGenerations;
  415.     end;
  416.  
  417.         {escape sequences}
  418.  
  419.  if (Ch = #27) and KeyPressed then {one more char?}
  420.     begin
  421.       Read(Kbd,Ch);
  422.  
  423.       if (Ch = #82) then        { ins }
  424.       begin       { Random Rule Random Inititialize }
  425.         Widen := 0;
  426.         GetRandomRule;
  427.         DisplayStatusLine;
  428.         InitializeAinitToBackground;
  429.         InitializeAinitRandom;
  430.         MoveAinitToAfield;
  431.         DisplayGenerations;
  432.       end;
  433.  
  434.       if (Ch = #83) then      { del }
  435.       begin      { Same Rule Random Inititialize }
  436.         Widen := 0;
  437.         DisplayStatusLine;
  438.         InitializeAinitToBackground;
  439.         InitializeAinitRandom;
  440.         MoveAinitToAfield;
  441.         DisplayGenerations;
  442.       end;
  443.  
  444.  
  445.         {function keys}
  446.  
  447.       if (Ch = #59) then                  { F1 }
  448.       begin
  449.       ChangeRule;
  450.       end;
  451.  
  452.       if (Ch = #60) then                  { F2 }
  453.       begin
  454.       SetBackground;
  455.       end;
  456.  
  457.       if (Ch = #61) then                  { F3 }
  458.       begin
  459.       end;
  460.  
  461.  
  462.       if (Ch = #66) then                  { F8 }
  463.       begin
  464.       Field80X47;
  465.       end;
  466.  
  467.       if (Ch = #67) then                  { F9 }
  468.       begin
  469.       field160X95;
  470.       end;
  471.  
  472.       if (Ch = #68) then                  { F10 }
  473.       begin
  474.       Field320X190;
  475.       end;
  476.  
  477.     end;  { if (Ch = #27 }
  478.   end;  { if keypressed }
  479. until Ch = #13;  { Return }  { end repeat }
  480.  
  481.  
  482. end.
  483.  
  484.  
  485.  
  486.